package com.jx.driving.message;

import java.io.Serializable;

import com.jx.driving.util.BCDUtiils;
import com.jx.driving.util.ByteUtils;

/**
 * 报文头
 * 
 * @author wuxiaozeng
 *
 */
public class MessageHeader implements Serializable {

	private int versionCode = 128;// 协议版本号
	private Integer messageId;// 消息 ID
	private MessageBodyProperty messageBodyProperty;// 消息体属性
	private String phone;// 终端手机号
	private int seq;// 消息流水号
	private int obligate;// 预留
	private Integer packageSum;// 总包数
	private Integer packageNumber;// 当前包数

	public MessageHeader() {
	}

	/**
	 * 构造方法，将byte转换成对象,取的是
	 */
	public MessageHeader(byte[] bytes) {
		this.versionCode = bytes[1] & 0xff;
		this.messageId = ByteUtils.bytes2int(new byte[] { 0, 0, bytes[2], bytes[3] });
		this.messageBodyProperty = new MessageBodyProperty(new byte[] { bytes[4], bytes[5] });
		this.phone = BCDUtiils.bcd2Str(
				new byte[] { bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13] });
		this.seq = ByteUtils.bytes2int(new byte[] { 0, 0, bytes[14], bytes[15] });
		this.obligate = bytes[16];
		if (this.messageBodyProperty.getSubPackageFlag()) {
			this.packageSum = ByteUtils.bytes2int(new byte[] { 0, 0, bytes[17], bytes[18] });
			this.packageNumber = ByteUtils.bytes2int(new byte[] { 0, 0, bytes[19], bytes[20] });
		}

	}

	/**
	 * 得到报文头，包含报文体的长度
	 */
	public byte[] getMessageHeader(byte[] messageBody) {
		byte[] header = null;
		if (getMessageBodyProperty().getSubPackageFlag()) {
			header = new byte[20];
			byte[] bytes = ByteUtils.intToBytes(packageSum);
			header[16] = bytes[2];
			header[17] = bytes[3];
			bytes = ByteUtils.intToBytes(packageNumber);
			header[18] = bytes[2];
			header[19] = bytes[3];
		} else {
			header = new byte[16];
		}
		header[0] = (byte) getVersionCode();
		header[1] = ByteUtils.intToBytes(getMessageId())[2];
		header[2] = ByteUtils.intToBytes(getMessageId())[3];
		header[3] = getMessageBodyProperty().getMessageBodyPropertyByteArray(messageBody)[2];
		header[4] = getMessageBodyProperty().getMessageBodyPropertyByteArray(messageBody)[3];
		byte[] phoneBCD = BCDUtiils.str2Bcd(getPhone());
		for (int i = 0; i < phoneBCD.length; i++) {
			header[5 + i] = phoneBCD[i];
		}
		header[13] = ByteUtils.intToBytes(getSeq())[2];
		header[14] = ByteUtils.intToBytes(getSeq())[3];
		header[15] = (byte) getObligate();
		return header;

	}

	/**
	 * 处理手机号码
	 * 
	 * @return
	 */
	public String getPhone() {
		StringBuilder sb = new StringBuilder();
		if (phone == null) {
			phone = "0000000000000000";
		}
		if (phone.length() < 16) {
			for (int i = 0; i < 16 - phone.length(); i++) {
				sb.append("0");
			}
		}
		return sb.append(phone).toString();
	}

	public int getVersionCode() {
		return versionCode;
	}

	public void setVersionCode(int versionCode) {
		this.versionCode = versionCode;
	}

	public Integer getMessageId() {
		return messageId;
	}

	public void setMessageId(Integer messageId) {
		this.messageId = messageId;
	}

	public MessageBodyProperty getMessageBodyProperty() {
		return messageBodyProperty;
	}

	public void setMessageBodyProperty(MessageBodyProperty messageBodyProperty) {
		this.messageBodyProperty = messageBodyProperty;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public int getObligate() {
		return obligate;
	}

	public void setObligate(int obligate) {
		this.obligate = obligate;
	}

	public Integer getPackageSum() {
		return packageSum;
	}

	public void setPackageSum(Integer packageSum) {
		this.packageSum = packageSum;
	}

	public Integer getPackageNumber() {
		return packageNumber;
	}

	public void setPackageNumber(Integer packageNumber) {
		this.packageNumber = packageNumber;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((messageBodyProperty == null) ? 0 : messageBodyProperty.hashCode());
		result = prime * result + ((messageId == null) ? 0 : messageId.hashCode());
		result = prime * result + obligate;
		result = prime * result + ((packageNumber == null) ? 0 : packageNumber.hashCode());
		result = prime * result + ((packageSum == null) ? 0 : packageSum.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		result = prime * result + seq;
		result = prime * result + versionCode;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MessageHeader other = (MessageHeader) obj;
		if (messageBodyProperty == null) {
			if (other.messageBodyProperty != null)
				return false;
		} else if (!messageBodyProperty.equals(other.messageBodyProperty))
			return false;
		if (messageId == null) {
			if (other.messageId != null)
				return false;
		} else if (!messageId.equals(other.messageId))
			return false;
		if (obligate != other.obligate)
			return false;
		if (packageNumber == null) {
			if (other.packageNumber != null)
				return false;
		} else if (!packageNumber.equals(other.packageNumber))
			return false;
		if (packageSum == null) {
			if (other.packageSum != null)
				return false;
		} else if (!packageSum.equals(other.packageSum))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		if (seq != other.seq)
			return false;
		if (versionCode != other.versionCode)
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("MessageHeader [versionCode=").append(versionCode).append(", messageId=").append(messageId)
				.append(", messageBodyProperty=").append(messageBodyProperty).append(", phone=").append(phone)
				.append(", seq=").append(seq).append(", obligate=").append(obligate).append(", packageSum=")
				.append(packageSum).append(", packageNumber=").append(packageNumber).append("]");
		return builder.toString();
	}

}
